package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
@Repository
public interface OrderMapper {
    void insert(Orders order);
    /**
     * 根据订单号和用户id查询订单
     * @param orderNumber
     * @param userId
     */
    @Select("select * from orders where number = #{orderNumber} and user_id= #{userId}")
    Orders getByNumberAndUserId(String orderNumber, Long userId);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 根据订单id查找订单信息
     * @return
     */
    @Select("select * from orders where id=#{id}")
    Orders getByOrderId(Long id);

    /**
     * 根据订单状态统计订单数
     * @param deliveryInProgress
     * @return
     */
    @Select("select count(*) from orders where status=#{deliveryInProgress}")
    Integer countStatus(Integer deliveryInProgress);
    @Update("update orders set status=#{status},pay_status=#{payStatus} where number=#{number} ")
    void updateStatusByOrderNumber(Orders orders);

    /**
     * 根据订单状态和下单时间查找订单
     * @param status
     * @param orderTime
     * @return
     */
    @Select("select * from orders where status=#{status} and order_time<#{orderTime}")
    List<Orders> selectByStatusAndOrderTime(Integer status, LocalDateTime orderTime);

    /**
     * @param beginTime 
     * @param endTime
     * @param status
     * @return
     */
    Double sumByStatusAndOrderTime(LocalDateTime beginTime, LocalDateTime endTime, Integer status);



    Integer countOrder(LocalDateTime beginTime, LocalDateTime endTime,Integer status);
    /**
     * 根据orderNumber查询订单
     */
    @Select("select id from orders where number=#{orderNumber}")
    Long getOrderIdByOrderNumber(String orderNumber);
}
